/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.conditions 
{
	import net.babeuf.form.fields.AbstractField;
	
	
	/**
	 * MatchCondition condition is used to check that two fields have the same value. 
	 * This can be useful for example if you want to be sure that a password and its confirm are the same.
	 * 
	 * @example
	 * <listing>
	 * var password : PasswordField = new PasswordField( myField ) ; 
	 * var confirm : PasswordField = new PasswordField( myFieldConfirm ) ; 
	 * confirm.notSent = true ;  
	 * 
	 * var condition : MatchCondition = new MatchCondition( password, confirm ) ; 
	 * 
	 * trace(condition.match()) ; // outputs true if the values are the same
	 * 
	 * </listing> 
	 * 
	 * @author damien pegoraro 
	 * 
	 * @see net.babeuf.form.fields.input.PasswordField
	 * @see net.babeuf.form.fields.input.EmailField
	 * 
	 * @langversion 3.0
	 * @playerversion Flash 10
	 */
	public class MatchCondition extends AbstractCondition implements ICondition
	{
		/**
		 * The first field to compare
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		protected var _field1 : AbstractField  ;
		
		/**
		 * The second field to compare
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		protected var _field2 : AbstractField  ;
		
	
		/**
		 * Creates a new instance
		 * 
		 * @param	field1  the first field to compare
		 * @param	field2 the second field to compare 
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		public function MatchCondition( field1 : AbstractField, field2 : AbstractField) 
		{
			_field1 = field1 ; 
			_field2 = field2 ; 	
		}
		
		/**		 
		 * @inheritDoc
		 */
		override public function match():Boolean 
		{
			super.match() ; 
			if (  _field1.getValue() != _field2.getValue() ) { _updateSensors(false) ; return false ; }
			return true ; 
		}
		
		/**
		 * @inheritDoc
		 */
		override public function dispose():void 
		{
			super.dispose();
			_field1 = null ; 
			_field2 = null ; 
		}
		
		
	}

}